import Fluent
import Vapor
var sockets = [WebSocket]()

func routes(_ app: Application) throws {
    app.get { req in
        
        return "It works!"
    }

    app.get("hello") { req -> String in
        return "Hello, world!"
    }

    // app.webSocket("echo",":uuid") { req, ws in
        
    //     if let uuid = req.parameters.get("uuid") {
    //         ws.identifier = uuid
    //         sockets.append(ws)
    //         ws.onText { ws, text in
    //             for socket in sockets {
    //                 socket.send("\(text):Someone send you a message!")
    //             }

    //         }
            
    //         ws.onClose.whenComplete { result in
    //             print("Something close!")
    //             sockets = sockets.filter { (webSocket) in
    //                 webSocket.identifier != ws.identifier
    //             }
    //         }
    //     }


    // }

    try app.register(collection: UserController())
    try app.register(collection: AdvancedBuildingController())
    try app.register(collection: ProjectController())
    try app.register(collection: SimpleBuildingController())
    try app.register(collection: MessageController())
    try app.register(collection: ReferenceController())
    try app.register(collection: ReferenceTextController())
    try app.register(collection: ReferenceImageController())
    try app.register(collection: ReferenceVideoController())
    try app.register(collection: LanguageController())
    try app.register(collection: CountryController())
    try app.register(collection: EmployeeController())
    try app.register(collection: DepartmentController())

}

// extension WebSocket {
    // private struct AssociatedKey {
    //     static var identifier: String = "identifier"
    // }
    
    // public var identifier: String {
    //     get {
    //         return objc_getAssociatedObject(self, &AssociatedKey.identifier) as? String ?? ""
    //     }
        
    //     set {
    //         objc_setAssociatedObject(self, &AssociatedKey.identifier, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
    //     }
    // }
// }
